import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import javax.swing.WindowConstants;

import chartRoom.Message;

import javax.swing.SwingUtilities;

/**dddd
 * hhhhhhhhhhhhhhhhh
啊啊啊啊啊啊啊啊啊啊啊
 *1.  服务器发送消息的时候将该消息写入到文件中
 *        缓冲字符流写入文件 小中大
 *2.  查看历史记录
 *    1. 显示 今天
 *       群发
 *       自己发的
 *       别人发给自己的
 */
public class LoginJFrame extends javax.swing.JFrame implements ActionListener {
	class xxx extends Thread {
		@Override
		public void run() {		
			while (true) {
				try {
					System.out.println("修main开始");
					sleep(60000);
					System.out.println("休眠结束");
					jButton1.setEnabled(false);
				    break;
					
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
		}
	}
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JButton jButton1;
	private JTextField jTextField2;
	private JLabel jLabel3;
	private JTextField jTextField1;
	private Socket s;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				LoginJFrame inst = new LoginJFrame();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
				

			}
		});
	}

	public LoginJFrame() {
		super();
		initGUI();
		//连接服务器
		try {
			s=new Socket("127.0.0.1",8888);
			JOptionPane.showMessageDialog(null,"服务器连接成功");
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		new xxx().start();

	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("ST1801即时通讯");
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("\u7528  \u6237  \u767b  \u5f55");
				jLabel1.setBounds(153, 38, 231, 64);
				jLabel1.setFont(new java.awt.Font("Microsoft YaHei", 0, 26));
			}
			{
				jLabel2 = new JLabel();
				getContentPane().add(jLabel2);
				jLabel2.setText("\u5e10\u53f7\uff1a");
				jLabel2.setBounds(122, 148, 64, 17);
			}
			{
				// 文本框对象
				jTextField1 = new JTextField();
				getContentPane().add(jTextField1);
				jTextField1.setBounds(198, 145, 153, 24);
			}
			{
				jButton1 = new JButton();
				getContentPane().add(jButton1);
				jButton1.setText("\u767b\u5f55");
				jButton1.setBounds(198, 260, 76, 35);
				jButton1.addActionListener(this);
			}
			{
				jLabel3 = new JLabel();
				getContentPane().add(jLabel3);
				jLabel3.setText("\u5bc6\u7801\uff1a");
				jLabel3.setBounds(122, 200, 54, 22);
			}
			{
	
				jTextField2 = new JTextField();
				getContentPane().add(jTextField2);
				jTextField2.setBounds(198, 201, 153, 22);
			}
			
		//	pack();
			setSize(500, 400);

			
		} catch (Exception e) {
			// add your error handling code here
			e.printStackTrace();
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// 处理登录请求的动作
		if (e.getSource() == jButton1) {
			/**
			 * 1） 取值 用户输入的数据
			 * 帐号对象   jTextField1
			 * 密码对象   jTextField2
			 */
			String name=jTextField1.getText().trim();
			//先服务器发送消息 请求登录
			try {
				//创建输出流
				OutputStream os=s.getOutputStream();
				ObjectOutputStream oos=new ObjectOutputStream(os);
				//将消息封装到对象中
				Message msg=new Message("denglu",name, null,null);
				//发送消息
				oos.writeObject(msg);
				oos.flush();//清空缓存，强制发送消息
				
				
				InputStream in=s.getInputStream();
				InputStreamReader isr=new InputStreamReader(in);
				BufferedReader br=new BufferedReader(isr);
				
				String str=br.readLine();
				if (str.equals("error")) {//登录失败
					JOptionPane.showMessageDialog(null,"账号重复，登录失败！");
				
				}else{//登录成功，跳转页面
					ClientJFrame inst = new ClientJFrame(name,s);
					inst.setLocationRelativeTo(null);
					inst.setVisible(true);
					this.dispose();//销毁当前页面
				}
				
				
				
				
			
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}

		

	}

}
